<?php
namespace app\common\model;
use app\common\model\Base;
class MallCategory extends Base
{
    protected $resultSetType = 'collection';

    public function getCategoryDetail($id='',$fields=''){
         return $this->where(['id'=>$id])->field($fields)->find();
    }

    /**
     * 查询多条数据
     * @return [type] [description]
     */
    public function getPageDatas($pid=0){
        return $this->getAllCates($pid);
    }
    /**
     * [deleteOneData 删除分类]
     * @param  string $data [description]
     * @return [type]       [description]
     */
    public function deleteOneData($id=''){
        $data = $this->where(['id'=>$id])->whereOr(['pid'=>$id])->select()->toArray();
        foreach ($data as $v) {
            $this->where(['id'=>$v['id']])->delete();
        }
        return ['data'=>'操作成功'];
    }

     /**
     * 新增分类
     * @param [type] $data [description]
     */
    public function addOneData($data=[]){
        return $this->save($data);
    }

    /**
     * 更新会员资料
     * @return [type] [description]
     */
    public function saveOneData($data=[],$id=''){
        return $this->save($data,['id' => $id]);
    }

    public function getAllCates($pid=0){
        $cates=$this->where(['pid'=>$pid])->select();
        $arr=array();
        foreach ($cates as $k => $v) {
            $v['child']=$this->getAllCates($v['id']);
            $v['tops'] = 1;
            if($this->getAllCates($v['id']) == null)$v['tops'] = 0;
            $arr[]=$v;
        }
        return $arr;
    }

}
